// pages/enterprise/user/setting/index.js
import Config from "../../../main.config";
import {getBindEmail} from "../../../service/Common";
import {getStorageSync, setStorageSync, removeStorageSync} from "../../../utils/Storage"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        email: "",
        phone: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let registerInfo = getStorageSync(Config.storage.registerInfo);
        this.data._userId = getStorageSync(Config.storage.userInfo + ".userId");
        this.data._rigisterId = registerInfo.id;
        this.setData({phone: registerInfo.phone});
        this.getBindEmail();
    },

    /*
    *
    * 获取简历接收邮箱
    * */
    getBindEmail: async function () {
        try {
            let userId = this.data._userId;
            if (!userId) {throw "getBindEmail: createUser can not empty"}
            let res = await getBindEmail(userId);
            this.setData({email: res.email});
            this.data._emailId = res.id;
        } catch (e) {
            console.error(e)
        }
    },


    /*
    * 跳转编辑详情页
    * */
    bindNavigatorToEdit: function (e) {
        const that = this
        let registerId = this.data._rigisterId;
        let email = this.data.email;
        let phone = this.data.phone;
        let emailId = this.data._emailId;
        let name = e.currentTarget.dataset.name;
        let url = "/packageEnterprise/pages/setting-edit/index?name=" + name;
        if (name === "email") {
            url = url
                + (emailId ? "&emailId=" + emailId : "")
                + (email ? "&email=" + email : "");
        } else {
            url = url + "&registerId=" + registerId + "&phone=" + phone
        }
        wx.navigateTo({
            url: url,
            events: {
                acceptDataFromOpenedPage: function(data) {
                    if (data.name === "email") {
                        that.setData({email: data.email});
                        that.data._emailId = data.emailId
                    } else {
                        that.setData({phone: data.phone});
                        setStorageSync(Config.storage.registerInfo + ".phone", data.phone);
                        setStorageSync(Config.storage.userInfo + ".phone", data.phone);
                    }
                }
            },
        })
    },

    /*
    * 登出
    * */
    onLogout: function () {
        removeStorageSync(Config.storage.token);
        wx.reLaunch({url: "/pages/login/index/index"})
    }

})